package rdb

import (
	"strings"

	"gorm.io/driver/mysql"
	"gorm.io/gorm"
	"gorm.io/gorm/logger"
)

type Config struct {
	Driver   string
	DSN      string
	LogLevel logger.LogLevel
	Default  bool
}

var DBs = map[string]*gorm.DB{}

func Init(configs map[string]Config) {
	for name, config := range configs {
		var db *gorm.DB
		var err error

		switch strings.ToLower(config.Driver) {
		case `mysql`:
			db, err = gorm.Open(mysql.Open(config.DSN), &gorm.Config{})
			if err != nil {
				panic(err)
			}

			db.Logger.LogMode(config.LogLevel)

			DBs[name] = db
		}
	}
}

func DB(name string) *gorm.DB {
	return DBs[name]
}
